home *** CD-ROM | disk | FTP | other *** search
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; formout - Library for formatted output.
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;
- ;;; Copyright (c) 1996 Harvey J. Stein <abel@netvision.net.il>, and
- ;;; eventually <hjstein@netvision.net.il>
- ;;; All Rights Reserved.
- ;;;
- ;;; This package is covered by the GNU GPL. You can freely use and
- ;;; distribute it as long as it stays under the GNU GPL, and as long as
- ;;; you distribute all the corresponding source code, and as long as this
- ;;; message and the above copyright notice remains.
- ;;;
- ;;; Usage:
- ;;; The point of this library is to pre-compile format statements so
- ;;; that output can be done quickly. The idea is similar to that of
- ;;; string->regexp. I had tried to use format from slib, but had
- ;;; found it too slow - presumably be cause each call to format has to
- ;;; parse things like "foo ~3,9s ~~...". This library is used by
- ;;; calling make-fmt-fcn with a format string. It returns a function
- ;;; of 2 arguments, a port and an object to output.
- ;;;
- ;;; A major drawback of this package is that it relies on a format
- ;;; implementation that understands ~s and ~a. I should have used
- ;;; display and write, but then it'd be more difficult to support a
- ;;; port of #f meaning to return a string.
-
- (define (realformout port data wid prec)
- (let* ((s (number->string data))
- (pnt (string-index "." s))
- (exp (string-index "e" s))
- (sl (string-length s)))
- (cond (exp
- (realformout port
- (string->number (substring s 0 exp))
- (- wid (- sl exp))
- (max 0(- prec (- sl exp))))
- (stringformoutnonquoted port
- (substring s exp sl)
- (- sl exp)))
- (pnt
- (stringformoutnonquoted port
- (string-append
- (substring s 0 (min sl (+ pnt prec 1)))
- (make-string (max 0 (- prec (- sl pnt 1))) #\0))
- (- wid))))))
-
- (define (stringformoutquoted port data wid)
- (let* ((l (string-length data))
- (p (make-string (max 0 (- (abs wid) l 2)) #\space)))
- (cond ((< wid 0)
- (format port "~a" p)
- (format port "~s" data))
- (else
- (format port "~s" data)
- (format port "~a" p)))))
-
-
- (define (stringformoutnonquoted port data wid)
- (let* ((l (string-length data))
- (p (make-string (max 0 (- (abs wid) l)) #\space)))
- (cond ((< wid 0)
- (format port "~a" (string-append p data)))
- (else
- (format port "~a" (string-append data p))))))
-
- (define (make-fmt-fcn s)
- (let* ((sl (string-length s))
- (at-pos (string-index "@" s))
- (comma-pos (string-index "," s))
- (typ (car (string->list (substring s (- sl 1) sl))))
- (wid-mult (if at-pos -1 1))
- (spec (if at-pos (substring s 1 at-pos)
- (substring s 1 (- sl 1))))
- (wid (* wid-mult (string->number
- (if comma-pos (substring spec 0 (- comma-pos 1))
- spec))))
- (prec (if comma-pos (string->number (substring spec
- comma-pos
- (string-length spec)))
- 0)))
- (case typ
- ((#\s #\S)
- (lambda (p d) (stringformoutquoted p d wid)))
- ((#\a #\A)
- (lambda (p d) (stringformoutnonquoted p d wid)))
- ((#\f #\F)
- (lambda (p d) (realformout p d wid prec))))))
-
-
-
-
- (provide "formout")
-